// Copyright (C) 1998-2008, Sumisho Computer Systems Corp. All Rights Reserved.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @author Hitoshi Okada
 */

package com.curlap.orb.servlet;

import com.curlap.orb.security.AccessControlAllow;
/**
 * CurlConfig
 */
public class CurlConfig 
{
    private Class serializerClass;
    private String objectManagementMethod;
    private InnerApplicationContext applicationContext;
    private InnerAuthentication authentication;
    //private String[] accessControlList;
    private AccessControlAllow[] accessControlList; 

    // constructors
    public CurlConfig()
    {
        serializerClass = null;
        objectManagementMethod = "both";
        applicationContext = null;
        authentication = null;
        accessControlList = null;
    }

    // Serializer
    public Class getSerializer()
    {
        return serializerClass;
    }
    public void setSerializer(Class serializerClass)
    {
        this.serializerClass = serializerClass;
    }
    
    // Object management
    /*
    public String getObjectManagementMethod()
    {
        return objectManagementMethod;
    }
    */
    public void setObjectManagementMethod(String objectManagementMethod)
    {
        this.objectManagementMethod = objectManagementMethod;
    }
    public boolean enabledHttpSessionMethod()
    {
        return (objectManagementMethod.equals("http-session") || objectManagementMethod.equals("both"));
    }
    public boolean enabledApplicationContext()
    {
        return (enabledOnlyApplicationContext() || objectManagementMethod.equals("both"));
    }
    public boolean enabledOnlyApplicationContext()
    {
        return (objectManagementMethod.equals("application-context"));
    }

    
    // Application Context
    public String getApplicationContextName()
    {
        return (applicationContext == null ? null : applicationContext.getName());
    }
    public Class getApplicationContextClass()
    {
        return (applicationContext == null ? null : applicationContext.getClazz());
    }
    public void setApplicationContext(String name, Class cls)
    {
        this.applicationContext = new InnerApplicationContext(name, cls);
    }

    // Authentication
    public String getAuthId()
    {
        return (authentication == null ? null : authentication.getId());
    }
    public Class getAuthClass()
    {
        return (authentication == null ? null : authentication.getAuthClass());
    }
    public Class getValidateClass()
    {
        return (authentication == null ? null : authentication.getValidateClass());
    }
    public void setAuthentication(String id, Class authClass, Class validateClass)
    {
        authentication = new InnerAuthentication(id, authClass, validateClass);
    }
    
    // ACL (Access Control List)
    public AccessControlAllow[] getAccessControlList()
    {
        return accessControlList;
    }
    public void setAccessControlList(AccessControlAllow[] accessControlList)
    {
        this.accessControlList = accessControlList;
    }


    // - - inner class - -
    
    class InnerApplicationContext
    {
        String name;
        Class cls;

        InnerApplicationContext(String name, Class cls)
        {
            this.name = name;
            this.cls = cls;
        }

        String getName()
        {
            return name;
        }

        Class getClazz()
        {
            return cls;  
        }
    }
    
    class InnerAuthentication
    {
        String id;
        Class authClass;
        Class validateClass;
        
        InnerAuthentication(String id, Class authClass, Class validateClass)
        {
            this.id = id;
            this.authClass = authClass;
            this.validateClass = validateClass;
        }
        
        String getId()
        {
            return id;
        }
        
        Class getAuthClass()
        {
            return authClass;
        }
        
        Class getValidateClass()
        {
            return validateClass;
        }
    }
}
